package cn.icare.cms.controller.restful;

import cn.icare.cms.ItemPicService;
import cn.icare.cms.ItemService;
import cn.icare.cms.model.enums.ItemStatusEnum;
import cn.icare.cms.model.ro.AddItemRO;
import cn.icare.cms.model.ro.CancelIsNewOrRecommendRo;
import cn.icare.cms.model.ro.EditItemRO;
import cn.icare.cms.model.ro.UpdateBatchItemStatusRo;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import cn.icare.cms.model.vo.rest.UserVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/item")
@Api(tags = "REST商品")
public class ItemController extends BaseController {
    @Resource
    private ItemService itemService;

    @Resource
    private ItemPicService itemPicService;

    private final String FALL_STRING = "请求失败,服务繁忙!";

    /**
     * URL:http://{ip}:{port}/rest/item
     * method:POST
     *
     * @param ro
     * @return
     */
    @PostMapping()
    @HystrixCommand(fallbackMethod = "createOneFall")

    @ApiOperation(value = "新增单个商品", notes = "t_item表新增单个商品")
    public ResponseEntity<RespMsgVO> createOne(@RequestBody AddItemRO ro,HttpServletRequest request) {
        UserVO systemUser=getCurrentUser(request);
        String systemUserName="";
        if(systemUser!=null)
        {
            systemUserName=systemUser.getUsername();
        }
        //商品防止重复检查
        boolean isExists= this.itemService.isExists(0L,ro.getCid(),ro.getCode(),ro.getTitle());
        if(isExists)
        {
            return super.getRespFail("商品编号或名称已经存在，不能重复添加");
        }
        Long id = this.itemService.createOne(ro,systemUserName);
        if (id >= 0) {
            //保存商品图片列表
            itemPicService.SavePicList(id,ro.getImageList(),false);
            return super.getRespOk(id);
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/item
     * method:PUT
     *
     * @param ro
     * @return
     */
    @PutMapping()
    @HystrixCommand(fallbackMethod = "updateOneFall")
    @ApiOperation(value = "更新单个商品", notes = "t_item表更新单个商品")
    public ResponseEntity<RespMsgVO> updateOne(@RequestBody EditItemRO ro, HttpServletRequest request) {
        UserVO systemUser=getCurrentUser(request);
        String systemUserName="";
        if(systemUser!=null)
        {
            systemUserName=systemUser.getUsername();
        }
        //商品防止重复检查
        boolean isExists= this.itemService.isExists(ro.getId(),ro.getCid(),ro.getCode(),ro.getTitle());
        if(isExists)
        {
            return super.getRespFail("商品编号或名称已经存在，不能重复添加");
        }
        if (this.itemService.updateOne(ro,systemUserName)) {

            //保存商品图片列表
            itemPicService.SavePicList(ro.getId(),ro.getImageList(),true);
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/item/{id}
     * method:DELETE
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @HystrixCommand(fallbackMethod = "deleteOneFall")
    @ApiOperation(value = "删除单个商品", notes = "t_item表删除符合主键的单个商品")
    public ResponseEntity<RespMsgVO> deleteOne(@PathVariable("id") Long id) {
        if (this.itemService.updateItemStatus(id, ItemStatusEnum.DELETE.getCode())) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/item?ids={ids}
     * method:DELETE
     *
     * @param ids
     * @return
     */
    @DeleteMapping()
    @HystrixCommand(fallbackMethod = "deleteBatchFall")
    @ApiOperation(value = "删除多个商品", notes = "t_item表删除符合主键的多个商品")
    public ResponseEntity<RespMsgVO> deleteBatch(@RequestParam("ids") List<Long> ids) {
        if (this.itemService.updateItemStatusBatch(ids,ItemStatusEnum.DELETE.getCode())) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/item/{id}
     * method:GET
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getOneFall")
    @ApiOperation(value = "查询单个商品", notes = "t_item表返回符合主键的单个商品")
    public ResponseEntity<RespMsgVO> getOne(@PathVariable("id") Long id) {
        return super.getRespOk(this.itemService.getOne(id));
    }

    /**
     * URL:http://{ip}:{port}/rest/item?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping()
    @ApiOperation(value = "查询多个商品", notes = "user表返回符合条件的多个商品")
    public ResponseEntity<RespMsgVO> getList(
            @RequestParam(name = "cid", required = false) Long cid,
            @RequestParam(name = "cpath", required = false) String cpath,
            @RequestParam(name = "isnew", required = false) Integer isnew,
            @RequestParam(name = "isrecommend", required = false) Integer isrecommend,
                                             @RequestParam(name = "id", required = false) String id,
                                             @RequestParam(name = "name", required = false) String name,
                                             @RequestParam(name = "pageNum", required = false, defaultValue = "${cms.defaultValue.pageNum}") Integer pageNum,
                                             @RequestParam(name = "pageSize", required = false, defaultValue = "${cms.defaultValue.pageSize}") Integer pageSize
                                             ) {
        return super.getRespOk(this.itemService.getList(cid,cpath,id,name, pageNum, pageSize,isnew,isrecommend));
    }




    /**
     * URL:http://{ip}:{port}/rest/item/{id}
     * method:GET
     *
     * @param id
     * @return
     */
    @GetMapping("/getitempics/{id}")

    @ApiOperation(value = "查询单个商品图片列表", notes = "查询单个商品图片列表")
    public ResponseEntity<RespMsgVO> getItemPics(@PathVariable("id") Long id) {

        List<String> pics=this.itemPicService.getPicList(id);
        return super.getRespOk(pics);
    }


    @PutMapping("/updateIsNewBatch")
    @ApiOperation(value = "批量更新商品新品状态", notes = "批量更新商品新品状态")
    public ResponseEntity<RespMsgVO> updateIsNewBatch(@RequestBody UpdateBatchItemStatusRo ro, HttpServletRequest request) {
        boolean result=this.itemService.updateIsNewBatch(ro.getIds(),ro.getStatus());
        if(result)
        {
            return super.getRespOk();
        }
        return super.getRespFail();
    }

    @PutMapping("/updateIsRecommendBatch")
    @ApiOperation(value = "批量更新商品推荐状态", notes = "批量更新商品推荐状态")
    public ResponseEntity<RespMsgVO> updateIsRecommendBatch(@RequestBody UpdateBatchItemStatusRo ro, HttpServletRequest request) {
        boolean result=this.itemService.updateIsRecommendBatch(ro.getIds(),ro.getStatus());
        if(result)
        {
            return super.getRespOk();
        }
        return super.getRespFail();
    }


    @PutMapping("/cancelIsNewOrRecommend")
    @ApiOperation(value = "取消某分类下所有商品新品或推荐", notes = "取消某分类下所有商品新品或推荐")
    public ResponseEntity<RespMsgVO> cancelIsNewOrRecommend(@RequestBody CancelIsNewOrRecommendRo ro) {

        boolean result=false;
        Long cid =ro.getCid();
        if(ro.getType()==1)
        {
            result=this.itemService.cancelIsNewBatch(cid);
        }
        else if(ro.getType()==2)
        {
            result=this.itemService.cancelIsRecommendBatch(cid);
        }
        if(result)
        {
            return super.getRespOk();
        }
        return super.getRespFail();
    }


    //#region fallback methods
    private ResponseEntity<RespMsgVO> createOneFall(AddItemRO ro,HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> updateOneFall(EditItemRO ro,HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteOneFall(Long id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteBatchFall(List<Long> ids) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getOneFall(Long id) {
        return super.getRespFail(this.FALL_STRING);
    }

    //#endregion

}
